<template>
    <view>
        <form-bo-list-preview @onRefresh="onRefresh" :key="key" ref="preview" :alias="alias" :pmtAlias="pmtAlias" :params="params" :formdata="formData"/>
    </view>
</template>

<script>
/**
 * 数据列表控件
 */
import MForm from "../js/MForm";
import FormBoListPreview from "@/views/modules/form/formBoList/FormBoListPreview";
import Util from '@/js/Util.js'
export default {
    name: "MDataList",
    mixins: [MForm],
    components: {
        FormBoListPreview
    },
    props: {},
    data() {
        return {
            alias: '',
            pmtAlias: '',
            params: '',
            queryAppId:'',
            formData:{},
            key:Util.randomId()
        }
    },
    created() {
        this.init();
    },
    mounted(){
        this.loadRelQueryData();
        this.handWatch();
    },
    methods: {
        init(){
            let {listKey, pmtAlias} = this.metadata.options;
            if (!listKey && !pmtAlias) {
                return;
            }
            this.alias =listKey;
            this.pmtAlias =pmtAlias;
            this.key=Util.randomId();
        },
        /**
         * 关联字段配置
         */
        loadRelQueryData(){
            this.formData={};
            let relConfig=this.metadata.options.relConfig;
            if(!relConfig){
                return;
            }
            let bindData= relConfig.bindData;
            if(bindData && bindData.length>0){
                for(let i=0;i<bindData.length;i++){
                    let field=bindData[i];
                    let fieldKey="";
                    //取表单主键
                    if(field.formField=="pkId"){
                        fieldKey=this.formVm.idField;
                    }else {
                        fieldKey=field.formField;
                    }
                    //转为json赋值
                    if(field.assign=="json") {
                        let object={
                            'value':this.data[fieldKey],
                            'label':this.data[field.formFieldLabel=="pkId"?this.formVm.idField:field.formFieldLabel]
                        };
                        this.formData[field.listField]=JSON.stringify(object);
                    }
                    //获取value
                    else if(field.assign=="value"){
                        if(this.data[fieldKey]){
                            this.formData[field.listField]=JSON.parse(this.data[fieldKey]).value;
                        }
                    }
                    //直接赋值
                    else {
                        this.formData[field.listField]=this.data[fieldKey];
                    }
                }
            }
            let params={};
            let typeJson={string:"S",int:"I",date:"D",float:"F"};
            //标记参数数量
            let num=0;
            if(relConfig.queryData && relConfig.queryData.length>0){
                for(let i=0;i<relConfig.queryData.length;i++){
                    let obj=relConfig.queryData[i];
                    if(!obj.autoFilter){
                        obj.autoFilter="YES";
                    }
                    let key=obj.autoFilter=='YES'?"Q_" + obj.name +"_" + typeJson[obj.dataType] + "_" + obj.fieldOp:obj.name;
                    let fieldKey=obj.val;
                    //取表单主键
                    if(fieldKey=="pkId"){
                        fieldKey=this.formVm.idField;
                    }
                    let val=this.data[fieldKey];
                    if(obj.valMode=="double" && val){
                        let objVal=JSON.parse(val);
                        val=objVal.value;
                    }
                    if(!val){
                        val=obj.defVal?obj.defVal:"";
                    }
                    params[key]=  val || "";
                    num++;
                }
                //只有参数都有值才做查询。
                if(num>0){
                    this.handParams(params);
                }
            }else {
                this.init();
            }
        },
        handParams(params) {
            let params_;
            if (typeof params == 'string') {
                params_ = JSON.parse(params);
            } else {
                params_ = params;
            }
            let queryParams = this.getQueryParams(params_);
            this.params = queryParams;
            this.init();
        },
        getQueryParams(query) {
            let ary = [];
            for (let key in query) {
                ary.push(key + "=" + query[key]);
            }
            return ary.join("&")
        },
        /**
         * 当表单数据变化时触发列表的数据变化。
         */
        handWatch(){
            let relConfig=this.metadata.options.relConfig;
            if(!relConfig.bindData || relConfig.bindData.length==0){
                return;
            }
            for(var i=0;i<relConfig.bindData.length;i++){
                let obj=relConfig.bindData[i];
                let self_=this;
                this.$watch("data."+obj.formField ,function (val) {
                    self_.loadRelQueryData();
                })
            }
        },
        onRefresh(){
            this.key=Util.randomId();
        }
    },
    watch: {},
}
</script>

<style scoped>

</style>
